package web.Courts;

import com.google.gson.Gson;
import entity.CourtsBean;
import services.CourtsServiceAll;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

@WebServlet("/readSettle")
public class ReadSettle extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        // 获取当前结算的时间
        Timestamp now = Timestamp.valueOf(LocalDateTime.now(ZoneId.systemDefault()));
        // 获取请求中的球桌名
        String courts_table = req.getParameter("courts_table");
        // 获取请求中的球桌id
        int court_id = Integer.parseInt(req.getParameter("court_id"));
        // 修改状态并接收受影响的行数
        int num = new CourtsServiceAll().updateStatusSettle(courts_table, 1, now);
        // 通过球桌id获取信息
        CourtsBean courtsByCourId = new CourtsServiceAll().getCourtsByCourId(court_id);
        // 获取开台的时间
        Instant courtsStartTime = courtsByCourId.getCourts_start_time().toInstant();
        //获取结算的时间
        Instant courtsEndTime = courtsByCourId.getCourts_end_time().toInstant();
        //获取时间差
        Duration difference = Duration.between(courtsStartTime, courtsEndTime);
        resp.getWriter().println(num > 0 ? difference.toMinutes() : "结算准备失败");
    }
}
